package com.googlecode.slm.service;

import java.util.List;

import com.googlecode.slm.daomapper.UserMapper;
import com.googlecode.slm.domain.dao.User;
import com.googlecode.slm.domain.web.UserNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by IntelliJ IDEA.
 * User: liguangjian
 * Date: 11-10-8
 * Time: 下午8:29
 * To change this template use File | Settings | File Templates.
 */
@Service
public class UserService {

    @Autowired
    @Qualifier(value = "userMapper")
    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Transactional
    public boolean reg(UserNode node){
        User user = new User();
        user.setName(node.getName());
        user.setPassword(node.getPassword());
        user.setGrade(node.getGrade());
        user.setEmail(node.getEmail());
        if (userMapper.reg(user)>0)
            return true;
        return false;
    }

    public UserNode login(UserNode node){
        User user = new User();
        user.setName(node.getName());
        user.setPassword(node.getPassword());
        user.setGrade(node.getGrade());
        user = userMapper.login(user);
        if (user!=null){
            node.setId(user.getId());
            node.setEmail(user.getEmail());
            return node;
        }
        return null;
    }
    
    public int getAllUser(int page, int rows, List<UserNode> list){
    	int start = (page-1)*rows; 
    	List<User> listUser = userMapper.getAll(start, rows);
    	int count = userMapper.countUser();
    	UserNode node = null;
    	for (User tem : listUser){
    		node = new UserNode();
    		node.setId(tem.getId());
    		node.setName(tem.getName());
    		node.setPassword(tem.getPassword());
    		node.setEmail(tem.getEmail());
    		node.setGrade(tem.getGrade());
    		list.add(node);
    	}
    	return count;
    }
    
    public int deleteUser(int id){
    	return userMapper.delete(id);
    }
}
